package com.uchengwang.ui.cm_ui.me.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.me.bean.AccessApplyBean;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserBean;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/7/26.
 */

public class AccessApplyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<AccessApplyBean> list = new ArrayList<>();
    private Handler handler;
    private List<RyUserBean> members;

    public AccessApplyAdapter(Context context, Handler handler){
        this.context = context;
        this.handler = handler;
    }

    public void setData(List<AccessApplyBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<AccessApplyBean> getData(){
        return list;
    }

    /**
     * 设置群成员
     * */
    public void setMembers(List<RyUserBean> members){
        this.members = members;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.apply_item, parent, false);
        return new AccessVh(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        final AccessVh holder = (AccessVh) h;

        final AccessApplyBean bean = list.get(position);

        String headUrl = bean.getUserAvatar();
        if (!TextUtils.isEmpty(headUrl)) {
            ImageManager.getInstance().loadImage(context, holder.apply_image, headUrl, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.apply_image, R.drawable.no_head, ImageManager.ImageType.CIRCLE);

        }
        holder.apply_name.setText(bean.getNickName());

//        String uniName = bean.getUniName();
//        holder.apply_info.setText(TextUtils.isEmpty(uniName) ? "暂无相关信息" : uniName);
        boolean isMem = isMember(bean.getUid());
        if (isMem){
            holder.apply_checkBox.setVisibility(View.INVISIBLE);
        }else {
            holder.apply_checkBox.setVisibility(View.VISIBLE);
        }

        holder.apply_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isCheck = bean.isCheck();
                holder.apply_checkBox.setChecked(!isCheck);
                bean.setCheck(!isCheck);
                if (handler != null){
                    Message msg = handler.obtainMessage(0);
                    msg.obj = !isCheck;
                    handler.sendMessage(msg);

                }
            }
        });

        holder.apply_checkBox.setChecked(bean.isCheck());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getUid());

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class AccessVh extends RecyclerView.ViewHolder{
        private View itemView;

        private ImageView apply_image;
        private TextView apply_name, apply_info;
        private AppCompatCheckBox apply_checkBox;
        private LinearLayout apply_ln;

        public AccessVh(View view) {
            super(view);

            AutoUtils.autoSize(view);

            itemView = view;
            apply_image = (ImageView) view.findViewById(R.id.apply_image);
            apply_name = (TextView) view.findViewById(R.id.apply_name);
            apply_info = (TextView) view.findViewById(R.id.apply_info);
            apply_ln = (LinearLayout) view.findViewById(R.id.apply_ln);
            apply_checkBox = (AppCompatCheckBox) view.findViewById(R.id.apply_checkBox);
            apply_ln.setVisibility(View.VISIBLE);
            apply_checkBox.setVisibility(View.VISIBLE);
            apply_checkBox.setClickable(false);
        }
    }

    /**
     * 判断是不是群成员
     * */
    public boolean isMember(long uid){
        if (members == null || members.size() <1){
            return false;
        }
        for (RyUserBean bean : members){
            if (bean.getMemberUid() == uid){
                return true;
            }
        }
        return false;
    }


}
